/*
游戏场景主要玩家节点，主要用来处理玩家信息，玩家牌型信息等
*/
var gameData = require("gameData");
var confige = require("confige");
var sanKungLogic = require("sanKungLogic");
var ZhaJinHuaLogic = require("ZhaJinHuaLogic");
var PaiGowLogic = require("PaiGowLogic");

cc.Class({
    extends: cc.Component,

    properties: {
    },

    onLoad: function () {
    },
    
    onInit:function(){
        gameData.gamePlayerNode = this;
        this.meChair = 0;
        this.noShowCardCount = 0;       //当前有多少人开牌
        this.playerCount = 0;           //当前参与游戏的人数,加入新玩家时该数字不会变,等新的一局开始时才会改变
        this.newPlayerCount = 0;
        this.playerList = {};
        for(var i=0;i<confige.playerMax;i++)
            this.playerList[i] = this.node.getChildByName("GamePlayer"+i);
        this.playerListNew = {};
        this.playerScoreList = {};
        for(var i=0;i<confige.playerMax;i++){
            this.playerListNew[i] = this.playerList[i].getComponent("playerNode");
            this.playerListNew[i].onInit();
            this.playerScoreList[i] = 0;
        }
        this.playerActiveList = {};
        this.playerCardList = {};
        this.playerHandCardList = {};

        this.playerListNew[0].setNick(confige.userInfo.nickname);
        this.playerListNew[0].setScore("");

        this.betNumNodeList = {};
        this.betNumLabelList = {};
        this.curBetNumList = {};
        this.isRobImgList = {};
        this.noRobImgList = {};
        this.lightBgList = {};
        this.watchCardImgList = {};
        this.failureImgList = {};
        this.discardImgList = {};
        this.isTurnImgList = {};
        this.leaveNodeList = {};
        this.robNumNodeList = {};
        this.robNumLabelList = {};

        this.cardItemList = this.node.getChildByName("cardListNode").getComponent("cardListNode");
        this.cardItemList.onInit();

        this.userInfoBtnList = this.node.getChildByName("userInfoBtnList");
        this.selectHead = -1;

        // this.sayBoxList = {};
        // this.sayBoxLabelNodeList = {};
        // this.sayBoxLabelList = {};
        // this.sayNode = this.node.getChildByName("sayList");
        // for(var i=0;i<confige.playerMax;i++)
        // {
        //     this.sayBoxList[i] = this.sayNode.getChildByName("sayBox"+(i+1));
        // }
        
        // for(var i=0;i<confige.playerMax;i++)
        // {
        //     this.sayBoxLabelNodeList[i] = this.sayBoxList[i].getChildByName("text");
        //     this.sayBoxLabelList[i] = this.sayBoxLabelNodeList[i].getComponent("cc.Label");
        // }
        // if(confige.playerMax == 6)  //对右边的玩家聊天框进行镜像翻转
        // {
        //     this.sayBoxList[1].rotationY = 180;
        //     this.sayBoxLabelNodeList[1].rotationY = 180;
        // }else{
        //     this.sayBoxList[1].rotationY = 180;
        //     this.sayBoxLabelNodeList[1].rotationY = 180;
        //     this.sayBoxList[2].rotationY = 180;
        //     this.sayBoxLabelNodeList[2].rotationY = 180;
        // }
        

        // this.faceList = {};
        // for(var i=0;i<confige.playerMax;i++)
        // {
        //     this.faceList[i] = this.sayNode.getChildByName("face"+(i+1));
        // }

        // this.sayAniNode = this.node.getChildByName("sayAniNode");
        // this.sayPosList = {};

        this.faceAniNode = this.node.getChildByName("faceAniNode");
        this.faceBeginPosList = {};
        for(var i=0;i<confige.playerMax;i++)
        {
            this.playerActiveList[i] = false;
            this.curBetNumList[i] = 0;
            var curHeadNode = this.playerList[i].getChildByName("userInfoNode").getChildByName("head");
            var newVec1 = this.playerList[i].getChildByName("userInfoNode").convertToWorldSpaceAR(cc.v2(curHeadNode.x,curHeadNode.y));
            var newVec2 = this.node.convertToNodeSpaceAR(cc.v2(newVec1.x,newVec1.y));
            this.faceBeginPosList[i] = {x:newVec2.x,y:newVec2.y};
            // this.sayPosList[i] = {x:newVec2.x,y:newVec2.y};
            gameData.gameBGNode.betBeginPosList[i] = {x:newVec2.x,y:newVec2.y};
        }
        if(confige.isBalcony)
        {
            this.sitBtnListNode = this.node.getChildByName("sitBtnList");
            this.sitBtnList = {};
            for(var i=0;i<confige.playerMax;i++)
                this.sitBtnList[i] = this.sitBtnListNode.getChildByName("sitBtn"+i);
        }
    },

    onStart:function(){
        console.log("gameScene Start!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        if(!confige.isBalcony)
            for(var i=0;i<confige.playerMax;i++)
                this.playerList[i].active = false;

        if(confige.roomPlayer != -1)
        {
            for(var i in confige.roomPlayer)
            {
                var newPlayerInfo = confige.roomPlayer[i];
                if(newPlayerInfo.uid == confige.userInfo.uid)
                {
                    this.meChair = parseInt(i);
                    gameData.gameMainScene.meChair = this.meChair;
                    confige.meChair = this.meChair;
                }
                confige.curPlayerMax ++;
            }
            for(var k in confige.roomPlayer)
            {
                if(confige.roomPlayer.hasOwnProperty(k))
                {
                    var curIndex = confige.getCurChair(parseInt(k));
                    var newPlayerInfo = confige.roomPlayer[k];
                    confige.curPlayerData[curIndex] = newPlayerInfo;
                    if(newPlayerInfo.isActive == true)
                    {
                        this.addOnePlayer(newPlayerInfo);
                        this.playerCount ++;
                        if(confige.isBalcony)
                            this.sitBtnList[curIndex] = false;
                        if(newPlayerInfo.isOnline == false)
                            this.playerListNew[confige.getCurChair(k)].setLeave();
                    }else{
                        if(confige.isBalcony)
                            this.sitBtnList[curIndex] = true;
                    }
                }
            }
        }
        
        if(this.playerCount == confige.playerMax)
            gameData.gameMainScene.gameInfoNode.btn_inviteFriend.active = false;

        this.newPlayerCount = this.playerCount;
    },

    addOnePlayer:function(playerData){
        console.log("addOnePlayer!!!!!playerData.chair ===== " + playerData.chair);
        var curIndex = confige.getCurChair(playerData.chair);
        console.log("addOnePlayer!!!!!chair ===== " + curIndex);
        console.log(playerData);
        this.playerListNew[curIndex].setNick(playerData.playerInfo.nickname);
        this.playerListNew[curIndex].setScore(playerData.score);

        if(playerData.playerInfo.head != "")
            this.playerListNew[curIndex].setHead(playerData.playerInfo.head);
        if(playerData.score)
            this.playerScoreList[parseInt(playerData.chair)] = playerData.score;

        if(playerData.isReady == true && confige.roomData.state == 1001)// confige.curReconnectData == -1 && )
        {
            this.playerListNew[curIndex].setReady();
            console.log("isReady.active = true");
        }else{
            console.log("isReady.active = false");
        }
        this.playerList[curIndex].active = true;
        confige.roomPlayer[playerData.chair] = playerData;
        confige.curPlayerData[curIndex] = playerData;
        this.newPlayerCount ++;
        console.log("addOnePlayer() this.newPlayerCount ==== " + this.newPlayerCount);
        confige.curPlayerCount ++;

        if(this.newPlayerCount == confige.playerMax)
            gameData.gameMainScene.gameInfoNode.btn_inviteFriend.active = false;
        this.playerActiveList[curIndex] = true;

        // this.faceAniList = {};
        this.faceAniNode = this.node.getChildByName("faceAniNode");
        // for(var i=1;i<=6;i++)
        //     this.faceAniList[i] = this.faceAniNode.getChildByName("faceAni"+i);
    },

    playerQuit:function(chair){
        console.log("playerQuit -------------------" + chair);
        var curIndex = confige.getCurChair(chair);
        this.playerList[curIndex].active = false;

        confige.roomPlayer[chair].isActive = false;
        confige.roomData.player[chair].isActive = false;
        confige.curPlayerData[curIndex] = confige.roomPlayer[chair];
        
        this.newPlayerCount --;
        confige.curPlayerCount --;
        if(this.newPlayerCount < confige.playerMax)
            gameData.gameMainScene.gameInfoNode.btn_inviteFriend.active = true;
        this.playerActiveList[curIndex] = false;
    },

    showOneCard:function(chair,callType){
        var curChair = confige.getCurChair(chair);
        if(curChair == 0)
        {
            gameData.gameMainScene.hideOpenCard(1);
            gameData.gameMainScene.hideOpenCard(2);
        }
        
        var handCard = this.playerCardList[chair];
        console.log("chair@@@@@"+curChair+"handCard ====");
        console.log(handCard);
        
        var curNiuType = 0;
        if(confige.roomData.roomType == "PaiGow" || confige.roomData.roomType == "PaiGowBalcony")
            curNiuType = PaiGowLogic.getType(handCard);
        if(confige.roomData.roomType == "sanKung")
            curNiuType = sanKungLogic.getType(handCard);
        
        console.log("curNiuType=====");
        console.log(curNiuType);
        if(window.localSetting.gameData.soundEnable && (confige.roomData.roomType == "PaiGow" || confige.roomData.roomType == "PaiGowBalcony"))
        {
            var curSex = 0;
            if(confige.roomPlayer[confige.getOriChair(chair)].playerInfo)
                curSex = parseInt(confige.roomPlayer[confige.getOriChair(chair)].playerInfo.sex);
            var curSoundIndex = curNiuType.type;
            if(curNiuType.type >= 1 && curNiuType.type <=9 && curNiuType.card.num == 10)
                curSoundIndex = ""+curSoundIndex+"B";
            if(chair == gameData.gameMainScene.curBankerChair)
            {
                window.DKTools.audioEG.playEffect("banker_type_"+curSoundIndex);
            }else{
                if(curSex == 2)
                    window.DKTools.audioEG.playEffect("female_type_"+curSoundIndex);
                else
                    window.DKTools.audioEG.playEffect("male_type_"+curSoundIndex);
            }
        }
        if(window.localSetting.gameData.soundEnable && confige.roomData.roomType == "sanKung")
        {
            var curSoundIndex = curNiuType.type;
            if(curSex == 2)
                window.DKTools.audioEG.playEffect("female_sankuang_type_"+curSoundIndex);
            else
                window.DKTools.audioEG.playEffect("male_sankuang_type_"+curSoundIndex);
        }
        // if(gameData.gameMainScene.joinLate == true && curChair == 0)
        //     return;
        var isShowB = false;
        if(confige.roomData.roomType == "PaiGow" || confige.roomData.roomType == "PaiGowBalcony")
            if(curNiuType.type >= 1 && curNiuType.type <=9 && curNiuType.card.num == 10)
                isShowB = true;
        this.playerListNew[curChair].showCardType(curNiuType.type,isShowB);
        var curCardNum = 0;
        for(var i in handCard)
        {
            if(handCard[i])
            {
                curCardNum ++;
                this.playerListNew[curChair].setCardWithIndex(i, handCard[i].num, handCard[i].type);
            }
        }
    },

    userDisconne:function(chair){
        this.playerListNew[chair].setLeave();
    },

    userReconnection:function(chair){
        this.playerListNew[chair].hideLeave();
    },

    showHeadFace:function(chairBegin,chairEnd,index,sex){
        if(confige.loadFaceAni == false) return;
        console.log("showHeadFace  chairBegin=" + chairBegin + "chairEnd=" + chairEnd + "index=" + index);
        var newFaceAni = cc.instantiate(confige.faceAniMap[index]);
        newFaceAni.scale = 0.7;
        newFaceAni.x = this.faceBeginPosList[confige.getCurChair(chairBegin)].x;
        newFaceAni.y = this.faceBeginPosList[confige.getCurChair(chairBegin)].y;
            if(sex == 2)
            {
                window.DKTools.audioEG.playEffect("female_face_"+index);
            }else{
                window.DKTools.audioEG.playEffect("male_face_"+index);
            }
        console.log("newFaceAni.x===" + newFaceAni.x);
        console.log("newFaceAni.y===" + newFaceAni.y);
        this.faceAniNode.addChild(newFaceAni);
        var action1 = cc.moveTo(0.3, cc.p(this.faceBeginPosList[confige.getCurChair(chairEnd)].x, this.faceBeginPosList[confige.getCurChair(chairEnd)].y));
        var action2 = cc.callFunc(function () {
                if(index == 3)
                {
                    if(sex == 2)
                        window.DKTools.audioEG.playEffect("face_3");
                    else
                        window.DKTools.audioEG.playEffect("face_7");
                }else{
                    window.DKTools.audioEG.playEffect("face_"+index);
                }
            newFaceAni.getComponent("cc.Animation").play("faceAni"+index);
        }, this);
        var action3 = cc.delayTime(1);
        var action4 = cc.fadeOut(0.3);
        var action5 = cc.callFunc(function () {
            newFaceAni.destroy();
        }, this);
        var betMoveAction = cc.sequence(action1,action2,action3,action4,action5);

        newFaceAni.runAction(betMoveAction);
    },

    updateScoreByChair:function(chair,score){
        this.playerScoreList[parseInt(chair)] = score;
        this.playerListNew[confige.getCurChair(chair)].setScore(score);
    },

    btnShowUserInfo:function(event,customEventData){
        console.log("btn_showUserInfo CLICK@@@@@@@@@@@@@")
        var clickIndex = parseInt(customEventData);
        var oriChair = confige.getOriChair(clickIndex);
        var isMe = false;
        if(oriChair == this.meChair)
            isMe = true;
        if(confige.roomPlayer[oriChair].isActive == true)
        {
            var newCallBack = function(){
                var ipString = confige.roomPlayer[oriChair].ip;
                ipString = ipString.substring(7, ipString.length);
                gameData.gameInfoNode.userInfoLayer.setUserInfo(confige.roomPlayer[oriChair].playerInfo.nickname,confige.roomPlayer[oriChair].playerInfo.uid,ipString);
                gameData.gameInfoNode.userInfoLayer.setHeadFrameWithURL(confige.roomPlayer[oriChair].playerInfo.head);
                gameData.gameInfoNode.userInfoLayer.selectHead = oriChair;
            };
            gameData.gameInfoNode.onBtnShowLayer(isMe,2,newCallBack);
        }
    },
});
